package com.business.business.controller;
import com.business.business.entity.OepreStore;
import com.business.business.entity.OepreVoca;
import com.business.business.entity.Result;
import com.business.business.service.OePreStoreService;
import com.business.business.service.OepreVocaService;
import com.business.business.util.FileUtil;
import com.business.business.util.MD5util;
import com.business.business.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Random;


@RestController
@RequestMapping("user")
public class OepreVocaController {
    @Autowired
    OepreVocaService oepreVocaService;
    @Autowired
    private OePreStoreService oePreStoreService;


    @PostMapping("dologin")
   public Result doLogin(@RequestBody OepreVoca oepreVoca) throws Exception{

       if (StringUtil.isEmpty(oepreVoca.getUsername())||StringUtil.isEmpty(oepreVoca.getPass())){

           return  Result.errorMsg("用户名密码不能为空");
       }

       OepreVoca oepreVocas =oepreVocaService.findOepreVocaByUsernameAndPass(oepreVoca.getUsername(), MD5util.getMD5Str(oepreVoca.getPass()));
        if (oepreVocas!=null){

            return Result.ok(oepreVocas);
        }

       return  Result.errorMsg("查无此人");
   }


   @GetMapping("login")
   public ModelAndView hello(){

        return  new ModelAndView("/login");
   }

    @GetMapping("index")
    public ModelAndView hellos(){

        return  new ModelAndView("/index");
    }

    @GetMapping("doAdd")
    public ModelAndView addStore(){

        return  new ModelAndView("/addStore");
    }


/*    @GetMapping("show")
        public ModelAndView showMe(@RequestParam String id, Model model) throws NumberFormatException {

        System.out.println(id);
       // List<OepreStore> oplist =oePreStoreService.findOepreStoreByVouserid(id);

       // model.addAttribute("oplist",oplist);

        return  new ModelAndView("/showme","storeModel",model);
    }*/




/*
    @GetMapping
    public ModelAndView showMe(@RequestParam(value = "async", required = false) boolean async,
                               @RequestParam(value = "id", required = false) String  id,
                               Model model) {

        System.out.println("================================================="+id);

         List<OepreStore> oplist =oePreStoreService.findOepreStoreByVouserid(Integer.parseInt(id));

        model.addAttribute("oplist",oplist);


        return new  ModelAndView(async == true ? "/showme  ::  #dtaContener" : "/showme","storeModel",model);
    }

*/








   @GetMapping("show")
    public ModelAndView shw(){

        return  new ModelAndView("/showme");
    }





    @PostMapping("doupload")
    public Result uploadPicture(@RequestParam(value="file",required=false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) {


        if (!file.isEmpty()) {
            // 获取文件名称,包含后缀
            String fileName = file.getOriginalFilename();

            String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());//文件后缀
            fileName = new Date().getTime() + "_" + new Random().nextInt(1000) + fileF;//新的文件名

            // 存放在这个路径下：该路径是该工程目录下的static文件下：(注：该文件可能需要自己创建)
            // 放在static下的原因是，存放的是静态文件资源，即通过浏览器输入本地服务器地址，加文件名时是可以访问到的
            String path = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/upload/";

            try {
                // 该方法是对文件写入的封装，在util类中，导入该包即可使用，后面会给出方法
                FileUtil.fileupload(file.getBytes(), path, fileName);
            } catch (IOException e) {

                e.printStackTrace();
                return Result.errorMsg("图片上传失败");
            }

            String url = "http://yw.aqbhj.com/upload/" + fileName;



            return Result.ok(url);
            // 接着创建对应的实体类，将以下路径进行添加，然后通过数据库操作方法写入

        }
        return Result.errorMsg("图片上传失败");

    }

}
